package com.niu.shop.service.admin.delivery.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.niu.core.common.domain.PageResult;
import com.niu.core.common.domain.PageParam;
import com.niu.shop.entity.delivery.ShopDeliveryElectronicSheet;
import com.niu.shop.mapper.delivery.ShopDeliveryElectronicSheetMapper;
import com.niu.shop.service.admin.delivery.param.ShopDeliveryElectronicSheetParam;
import com.niu.shop.service.admin.delivery.param.ShopDeliveryElectronicSheetSearchParam;
import com.niu.shop.service.admin.delivery.IShopDeliveryElectronicSheetService;
import com.niu.shop.service.admin.delivery.vo.ShopDeliveryElectronicSheetInfoVo;
import com.niu.shop.service.admin.delivery.vo.ShopDeliveryElectronicSheetListVo;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;
import javax.annotation.Resource;
import java.util.LinkedList;
import java.util.List;


/**
 * 电子面单实现
 */
@Service
public class ShopDeliveryElectronicSheetServiceImpl implements IShopDeliveryElectronicSheetService {
        
    @Resource
    ShopDeliveryElectronicSheetMapper shopDeliveryElectronicSheetMapper;

    /**
     * 电子面单列表
     * @param pageParam 分页参数
     * @param searchParam 搜索参数
     * @return PageResult<ShopDeliveryElectronicSheetListVo>
     */
    @Override
    public PageResult<ShopDeliveryElectronicSheetListVo> list(PageParam pageParam, ShopDeliveryElectronicSheetSearchParam searchParam) {
        Integer page  = pageParam.getPage();
        Integer limit =pageParam.getLimit();

        QueryWrapper<ShopDeliveryElectronicSheet> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByDesc("id");

        IPage<ShopDeliveryElectronicSheet> iPage = shopDeliveryElectronicSheetMapper.selectPage(new Page<>(page, limit), queryWrapper);
        List<ShopDeliveryElectronicSheetListVo> list = new LinkedList<>();
        for (ShopDeliveryElectronicSheet item : iPage.getRecords()) {
            ShopDeliveryElectronicSheetListVo vo = new ShopDeliveryElectronicSheetListVo();
            BeanUtils.copyProperties(item, vo);
            list.add(vo);
        }
        return PageResult.build(page, limit, iPage.getTotal()).setData(list);
    }

    /**
     * 电子面单详情
     * @param id 主键
     * @return ShopDeliveryElectronicSheetInfoVo
     */
    @Override
    public ShopDeliveryElectronicSheetInfoVo info(Integer id) {
        ShopDeliveryElectronicSheet model = shopDeliveryElectronicSheetMapper.selectOne(
                new QueryWrapper<ShopDeliveryElectronicSheet>()
                    .eq("id", id)
                    .last("limit 1"));

        Assert.notNull(model, "数据不存在");

        ShopDeliveryElectronicSheetInfoVo vo = new ShopDeliveryElectronicSheetInfoVo();
        BeanUtils.copyProperties(model, vo);
        return vo;
    }

    /**
     * 电子面单添加
     * @param addParam 参数
     */
    @Override
    public void add(ShopDeliveryElectronicSheetParam addParam) {
        ShopDeliveryElectronicSheet model = new ShopDeliveryElectronicSheet();
        model.setSiteId(addParam.getSiteId());
        model.setTemplateName(addParam.getTemplateName());
        model.setExpressCompanyId(addParam.getExpressCompanyId());
        model.setCustomerName(addParam.getCustomerName());
        model.setCustomerPwd(addParam.getCustomerPwd());
        model.setSendSite(addParam.getSendSite());
        model.setSendStaff(addParam.getSendStaff());
        model.setMonthCode(addParam.getMonthCode());
        model.setPayType(addParam.getPayType());
        model.setIsNotice(addParam.getIsNotice());
        model.setStatus(addParam.getStatus());
        model.setExpType(addParam.getExpType());
        model.setPrintStyle(addParam.getPrintStyle());
        model.setIsDefault(addParam.getIsDefault());
        model.setCreateTime(System.currentTimeMillis() / 1000);
        model.setUpdateTime(System.currentTimeMillis() / 1000);
        shopDeliveryElectronicSheetMapper.insert(model);
    }

    /**
     * 电子面单编辑
     *
     * @param editParam 参数
     */
    @Override
    public void edit(Integer id, ShopDeliveryElectronicSheetParam editParam) {
        ShopDeliveryElectronicSheet model = shopDeliveryElectronicSheetMapper.selectOne(
                new QueryWrapper<ShopDeliveryElectronicSheet>()
                    .eq("id",  id)
                    .last("limit 1"));

        Assert.notNull(model, "数据不存在!");
        model.setId(id);
        
        model.setSiteId(editParam.getSiteId());
        model.setTemplateName(editParam.getTemplateName());
        model.setExpressCompanyId(editParam.getExpressCompanyId());
        model.setCustomerName(editParam.getCustomerName());
        model.setCustomerPwd(editParam.getCustomerPwd());
        model.setSendSite(editParam.getSendSite());
        model.setSendStaff(editParam.getSendStaff());
        model.setMonthCode(editParam.getMonthCode());
        model.setPayType(editParam.getPayType());
        model.setIsNotice(editParam.getIsNotice());
        model.setStatus(editParam.getStatus());
        model.setExpType(editParam.getExpType());
        model.setPrintStyle(editParam.getPrintStyle());
        model.setIsDefault(editParam.getIsDefault());
        model.setUpdateTime(System.currentTimeMillis() / 1000);
        shopDeliveryElectronicSheetMapper.updateById(model);
    }

    /**
     * 电子面单删除
     * @param id 主键ID
     */
    @Override
    public void del(Integer id) {
        ShopDeliveryElectronicSheet model = shopDeliveryElectronicSheetMapper.selectOne(
                new QueryWrapper<ShopDeliveryElectronicSheet>()
                    .eq("id", id)
                    .last("limit 1"));

        Assert.notNull(model, "数据不存在!");

        shopDeliveryElectronicSheetMapper.delete(new QueryWrapper<ShopDeliveryElectronicSheet>().eq("id", id));
    }

}
